﻿Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Partial Public Class CHONTOUR
    Inherits Office2007Form

    Private _selectedTour As Integer
    Public Event Handler(ByVal sender As Object, ByVal e As MyEvent)

    Public Sub LoadData()
        treeView1.Nodes.Clear()
        Dim root As TreeNode = New TreeNode("Danh mục tour du lịch")
        root.ImageIndex = 0
        treeView1.Nodes.Add(root)

        Try
            Dim objBus As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objBus.SelectAll().Tables(0)
            If lstItem.Rows.Count > 0 Then
                For i As Integer = 0 To lstItem.Rows.Count - 1
                    Dim tourNode As TreeNode = New TreeNode(lstItem.Rows(0)("ID").ToString() + " - " + lstItem.Rows(i)("TenTour").ToString())
                    tourNode.ImageIndex = 1
                    root.Nodes.Add(tourNode)
                Next
            End If
        Catch ex As Exception
            Throw New Exception(ex.Message, ex.InnerException)
        End Try
    End Sub

    Public Function GetLevelTreeView(ByVal node As TreeNode) As Integer
        If node.Parent Is Nothing Then
            Return 1
        ElseIf node.Parent.Parent Is Nothing Then
            Return 2
        End If
    End Function

    Private Sub treeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeView1.AfterSelect
        Dim level As Integer = GetLevelTreeView(treeView1.SelectedNode)
        If level = 2 Then
            btChon.Enabled = True
            _selectedTour = treeView1.SelectedNode.Text.Split(New Char() {"-"c})(0).Trim()
        Else
            btChon.Enabled = False
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub btChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btChon.Click
        Dim objBus As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
        Dim lstItem As DataTable = objBus.SelectByID(Integer.Parse(_selectedTour)).Tables(0)

        Close()
        RaiseEvent Handler(Me, New MyEvent(lstItem.Rows(0)("ID").ToString() & "#" & lstItem.Rows(0)("TenTour").ToString()))
    End Sub

    Private Sub CHONTOUR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub
End Class